package com.spark.data

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.google.gson.Gson
import com.spark.colliery.common.ext.gone
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.report
import com.spark.common.ext.sdf
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.data.model.NotifyHistoryBorrowListModel
import kotlinx.android.synthetic.main.frag_borrow_apply_result.*

/**
 * maybe the administer will agree the apply of the data
 */
class BorrowApplyResultFragment : BaseFragment() {
    private var taskId: String = ""
    private var messageType: String = ""

    companion object {
        fun newInstance(taskId: String, messageType: String): BorrowApplyResultFragment {
            val target = BorrowApplyResultFragment()
            val args = Bundle()
            args.putString("taskId", taskId)
            args.putString("messageType", messageType)
            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_borrow_apply_result
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }
        arguments?.apply {
            taskId = getString("taskId")
            messageType = getString("messageType")
        }
    }

    private fun updateView(data: NotifyHistoryBorrowListModel) {
        data.resultMaps[0]?.apply {
            tv_save.gone(true)
            tv_state.text = this.taskstateFlag
            tv_file_name.text = this.name
            tv_regular_name.text = this.name
            tv_serial.text = this.name
            tv_borrow_days.text =sdf().format(this.endTime)
            tv_reason.text = this.name
        }
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        fetchInfo()
    }

    @SuppressLint("CheckResult")
    private fun fetchInfo() {
        val params = mutableMapOf(
                Pair(Const.qr_taskId, taskId),
                Pair(Const.qr_messageType, messageType)
        )
        Http.request()
                .rxPost("${Const.base_url}${Const.act_notify_history_file_info}", params.toJsonBody())
                .wrapIO()
                .subscribe(
                        {
                            Gson().fromJson(it, NotifyHistoryBorrowListModel::class.java)?.apply {
                                if (code == "200") {
                                    updateView(this)
                                } else {
                                    error(msg)
                                }
                            }
                        },
                        {
                            hideLoader()
                            it.printStackTrace()
                            report(it)
                        },
                        {
                            hideLoader()
                        },
                        {
                            dispose = it
                            loading()
                        }
                )

    }
}